package com.leiqichao.backend.controller;

import com.leiqichao.backend.common.Result;
import com.leiqichao.backend.entity.Tag;
import com.leiqichao.backend.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/tags")
public class TagController {

    @Autowired
    private TagService tagService;

    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    /**
     * 获取所有标签列表
     */
    @GetMapping
    public Result<?> getAllTags() {
        return Result.success(tagService.list());
    }

    /**
     * 创建标签
     */
    @PostMapping
    public Result<Tag> createTag(@RequestBody Tag tag) {
        if (tagService.createTag(tag)) {
            return Result.success(tag);
        }
        return Result.error(500, "创建失败");
    }

    /**
     * 更新标签
     */
    @PutMapping("/{id}")
    public Result<Tag> updateTag(@PathVariable Long id, @RequestBody Tag tag) {
        tag.setId(id);
        if (tagService.updateTag(tag)) {
            return Result.success(tag);
        }
        return Result.error(500, "更新失败");
    }

    /**
     * 删除标签（逻辑删除）
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteTag(@PathVariable Long id) {
        return tagService.removeById(id) ? 
            Result.success() : Result.error(500, "删除失败");
    }
}